<?php
namespace application\admin\panels;
use application\utilities\Filesystem;
class update extends panel {
	public $name = 'Update Concerto';
	public function title() {
		return 'Update Concerto';
	}
	
	public function sidebar() {
	
	}
	
	public function display() {
		
		?>
		<div class="box">
			<h3>Update Concerto</h3>
			<div class="box-content">	
				<?php
				$latestVersion = file_get_contents('http://concerto.torchdm.com/download/latest/version.txt');
				$currentVersion = getOption('current_version');
				if($_REQUEST['action'] == 'update') {
					
					if($latestVersion > $currentVersion) {
						
						/** Download zip file */
						$download = 'http://concerto.torchdm.com/download/latest/concerto_update.zip';
						copy($download, CN_APPLICATION_PATH.'concerto_update.zip');
						Filesystem::unzipFile(CN_APPLICATION_PATH.'concerto_update.zip', CN_APPLICATION_PATH);
						Filesystem::recursiveCopy(CN_APPLICATION_PATH.'concerto_update', CN_APPLICATION_PATH);
						Filesystem::recursiveDelete(CN_APPLICATION_PATH.'concerto_update');
						unlink(CN_APPLICATION_PATH.'concerto_update.zip');
						setOption('just_updated', $currentVersion);
						echo '<p>Upgrade complete! Please refresh this page to see all changes.</p>';
						setOption('current_version', $latestVersion);
					}
				}
				else { ?>
				<?php if($currentVersion < $latestVersion) : ?>
				<b>There is a new version of Concerto available (<?=$latestVersion?>). Click <a href="javascript:concerto.admin.loadPanel('update', {action:'update'});">here</a> to automatically install the update.</b>
				<?php else : ?>
				You have the latest version of Concerto.
				<?php endif;?>
				<?php } ?>
			</div>
		</div>
		<?php
	}
}
?>